package is.progetto2.activity;

import is.progetto2.activity.abstraction.BindFactory;
import is.progetto2.activity.abstraction.JobFactory;

import java.util.LinkedList;
import java.util.List;

public class Activity {

	private List<Job> jobs;
	private List<Bind> binds;
	private JobFactory jf;
	private BindFactory bf;

	public Activity() {
		this.jobs = new LinkedList<>();
		this.binds = new LinkedList<>();
		jf=JobFactoryImp.getDefaultInstance();
		bf=BindFactoryImpl.getDefaultInstance();
	}

	public Job createJob(int duration){
		Job j=jf.createJob(duration);
		this.add(j);
		return j;
	}
	
	public Bind createBind(BindType b1, Job j1, BindType b2, BindType b3, Job j2){
		Bind b=bf.createBind(b1, j1, b2, b3, j2);
		this.addBind(b);
		return b;
	}
	
	public void removeJob(Job j){
		this.jf.remove(j);
		Bind tmp=null;
		for(Bind b:binds){
			if(b.getJob1().equals(j)){
				this.bf.deleteBindRef(b);
				tmp=b;
				break;
			}
		}
		this.binds.remove(tmp);
		this.remove(j);
	}
	
	public void removeBind(Bind b){
		this.remove(b);
	}
	
	protected void add(Job j) {
		this.jobs.add(j);
	}

	protected void addBind(Bind b) {
		this.binds.add(b);
	}

	public boolean contains(Job j) {
		return this.jobs.contains(j);
	}

	public boolean contains(Bind b) {
		return this.binds.contains(b);
	}

	protected void remove(Job j) {
		this.jobs.remove(j);
	}

	protected void remove(Bind b) {
		bf.deleteBindRef(b);
		this.binds.remove(b);
	}
	
	protected ActivitySorted getActivitySorted(){
		return new ActivitySorted(this.jobs);
	}
	
	public Job getJob(int i){
		return jobs.get(i);
	}
	
	public int getJobSize(){
		return this.jobs.size();
	}

	@Override
	public String toString() {
		return "Activity [jobs=" + jobs + ", binds=" + binds + "]";
	}

	public int getBindSize() {
		return this.binds.size();
	}

	public Bind getBind(int arg0) {
		return this.binds.get(arg0);
	}
}
